/**
 * . ssi<br/>
 * Copyright © 2010 HopeRun Inc. All Rights Reserved.
 */

package com.bj.jiufu.util.crypto;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

/**
 * 
 * 使用DES加密&解密类
 * 
 * @author wang_zhongpei
 * @function
 * @version 2010-12-4 下午10:48:28
 */
public class DesUtil {

    /** 加密所使用的密钥 */
    private static final String PASSWORD_CRYPT_KEY = "12312312";
    /** 加密种子 */
    private static final String DES = "DES";
    /** 数字2 */
    private static final int TWO = 2;

    /**
     * 默认的构造方法
     */
    private DesUtil() {

    }

    public static byte[] encrypt(byte[] src, byte[] key)
        throws InvalidKeyException, NoSuchAlgorithmException,
            InvalidKeySpecException, NoSuchPaddingException,
            IllegalBlockSizeException, BadPaddingException {
        SecureRandom sr = new SecureRandom();
        DESKeySpec dks = new DESKeySpec(key);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance(DES);
        cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
        return cipher.doFinal(src);
    }


    public static byte[] decrypt(byte[] src, byte[] key)
        throws InvalidKeyException, NoSuchAlgorithmException,
            InvalidKeySpecException, NoSuchPaddingException,
            IllegalBlockSizeException, BadPaddingException {
        SecureRandom sr = new SecureRandom();
        DESKeySpec dks = new DESKeySpec(key);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance(DES);
        cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
        return cipher.doFinal(src);
    }

    /**
     * 
     * 密码解密
     * 
     * @param data
     *            需要解密的数据
     * 
     * @return 解密后的明文数据
     */

    public static final String decrypt(String data) {

        try {
            return new String(decrypt(hex2byte(data.getBytes()),
                    PASSWORD_CRYPT_KEY.getBytes()));
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 
     * 密码加密
     * 
     * @param password
     *            明文密码
     * 
     * @return 加密后的数据
     * 
     */
    public static final String encrypt(String password) {
        try {
            return byte2hex(encrypt(password.getBytes(),
                    PASSWORD_CRYPT_KEY.getBytes()));
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 加密字符数组
     * 
     * @param bytes
     *            待加密的字符数组
     * @return 加密后的字符数组
     */
    private static String byte2hex(byte[] bytes) {
        final int tempBytes = 0XFF;
        if (bytes != null) {
            String stmp = "";
            StringBuilder sb = new StringBuilder();
            for (int n = 0; n < bytes.length; n++) {
                stmp = java.lang.Integer.toHexString(bytes[n] & tempBytes);
                if (stmp.length() == 1) {
                    sb.append("0").append(stmp);
                } else {
                    sb.append(stmp);
                }
            }
            return sb.toString().toUpperCase();
        } else {
            return null;
        }
    }

    /**
     * 加密字符数组
     * 
     * @param bytes
     *            待加密的字符数组
     * @return 加密后的字符数组
     */
    private static byte[] hex2byte(byte[] bytes) {
        final int index = 16;
        if (bytes != null) {
            if ((bytes.length % TWO) != 0) {
                throw new IllegalArgumentException("长度不是偶数");
            }
            byte[] b2 = new byte[bytes.length / TWO];
            for (int n = 0; n < bytes.length; n += TWO) {
                String item = new String(bytes, n, TWO);
                b2[n / TWO] = (byte) Integer.parseInt(item, index);
            }
            return b2;
        } else {
            return null;
        }
    }
}
